import operate from '@dfeidao/atom-nodejs/db/operate';
import uuid from 'uuid';

// import { IBaseTable } from '@feidao/web/atom/nodejs/db/interfaces';
import query from '@dfeidao/atom-nodejs/db/query';
interface Message {
	role_allot_id: string;
	role_no: string;
	productid: string;
}
interface Data {
	priority: number;
}
export default async function atom(msg: Message, action_id: string, session_id: string, spaceid: string) {
	const systemid = '12';
	const { exec, insert } = operate(action_id, session_id, systemid);
	const { exec: e, prepare: p } = query(action_id, session_id, systemid);
	const productid = msg.productid;
	const role_allot_id = msg.role_allot_id;
	p('allot_conditions_group', ['priority'], { productid, role_allot_id }, 200, 1, ['priority'], []);
	const [data] = await e<Data>();
	const arr = data.map((v) => {
		return v.priority;
	});
	// console.log(arr)
	let priority = 0;
	if (arr.length !== 0) {
		priority = arr[arr.length - 1] + 1;
	}
	const _ids = uuid();

	// console.log(priority)
	insert('allot_conditions_group', { role_allot_id: msg.role_allot_id, role_no: msg.role_no, productid: msg.productid, _id: _ids, group_id: uuid(), parent_group_id: "", connector: "且", priority });
	const res = await exec();
	if (res.code === 1) {
		return { str: 'success', _ids };
	} else {
		return { str: 'fail' };
	}
}
